from flask import Flask
from flask_cors import CORS
import os

# 创建Flask应用实例
template_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'templates'))
static_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'templates', 'static'))
app = Flask(__name__, 
           template_folder=template_dir,
           static_folder=static_dir)

# 确保输出目录存在
output_dir = os.path.join(os.getcwd(), 'output')
try:
    os.makedirs(output_dir, exist_ok=True)
    print(f"✅ 已创建输出目录: {output_dir}")
except Exception as e:
    print(f"❌ 无法创建输出目录: {str(e)}")
    raise

# 启用CORS
CORS(app)

# 导入和注册蓝图
from app.routes import routes, teardown
app.register_blueprint(routes)
app.teardown_appcontext(teardown)
